package com.tancire.question.controller;

import com.tancire.common.annotation.RateLimiter;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.enums.LimitType;
import com.tancire.question.domain.Question;
import com.tancire.question.domain.QuestionCategory;
import com.tancire.question.domain.QuestionSector;
import com.tancire.question.service.IQuestionCategoryService;
import com.tancire.question.service.IQuestionSectorService;
import com.tancire.question.service.IQuestionService;
import com.tancire.question.utils.OpenAuthUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 开放接口
 * 面试题在线查询
 * 【手机端】
 */
@RestController
@RequestMapping("/open/mobile/question")
public class QuestionOpenController extends BaseController {

    @Resource
    private IQuestionSectorService questionSectorService;

    @Resource
    private IQuestionCategoryService questionCategoryService;

    @Resource
    private IQuestionService questionService;

    @Resource
    private OpenAuthUtils openAuthUtils;

    /**
     * 查询面试岗位列表
     */
    @RateLimiter(key = "/sector/list", time = 1, count = 2, limitType = LimitType.IP)
    @GetMapping("/sector/list")
    public AjaxResult list(QuestionSector questionSector) {
        // 判断是否配置开放接口
        this.openAuthUtils.assertApiOpen();
        // 执行逻辑
        List<QuestionSector> list = questionSectorService.selectQuestionSectorList(questionSector);
        // 记录日志
        this.openAuthUtils.saveRequestLog("sectorList", "查询面试岗位列表", null);
        return AjaxResult.success("查询成功", list);
    }

    /**
     * 查询问题分类列表(全查)
     */
    @RateLimiter(key = "/category/list", time = 1, count = 2, limitType = LimitType.IP)
    @GetMapping("/category/list")
    public AjaxResult listAll(QuestionCategory questionCategory) {
        // 判断是否配置开放接口
        this.openAuthUtils.assertApiOpen();
        Long sectorId = questionCategory.getSectorId();
        if (null == sectorId) {
            return AjaxResult.error("所属行业不能为空");
        }
        // 执行逻辑
        List<QuestionCategory> list = questionCategoryService.selectQuestionCategoryList(questionCategory);
        // 记录日志
        this.openAuthUtils.saveRequestLog("categoryList", "查询分类列表", null);
        return AjaxResult.success("查询成功", list);
    }

    /**
     * 查询面试题列表
     */
    @RateLimiter(key = "/list", time = 1, count = 2, limitType = LimitType.IP)
    @GetMapping("/list")
    public TableDataInfo list(Question question) {
        // 判断是否配置开放接口
        this.openAuthUtils.assertApiOpen();
        // 分页查询
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        // 记录日志
        this.openAuthUtils.saveRequestLog("questionList", "查询面试题列表", null);
        return getDataTable(list);
    }

}
